<template>
  <div class="mod-poap__poapparticipantproject">
    <el-drawer
      v-model="state.drawerPart"
      title="合作者"
      size="90%"
      direction="rtl"
      :before-close="handleClose"
    >
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
<!--      <el-form-item>-->
<!--        <el-input v-model="state.dataForm.participantId" placeholder="合作者id(user_id)" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-input v-model="state.dataForm.projectId" placeholder="项目id" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="info" @click="state.getDataList()">刷新</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('poap:poapparticipantproject:save')" type="primary" @click="addOrUpdateHandle()" v-show="state.buttonShow">邀请合作者</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('poap:poapparticipantproject:delete')" type="danger" @click="state.deleteHandle()"  v-show="state.buttonShow">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="participant.username" label="合作者" header-align="center" align="center"></el-table-column>
      <el-table-column prop="project.name" label="项目" header-align="center" align="center"></el-table-column>
      <el-table-column prop="roleText" label="项目职责" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150"  v-if="state.buttonShow">
        <template v-slot="scope">
<!--          <el-button v-if="state.hasPermission('poap:poapparticipantproject:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>-->
          <el-button v-if="state.hasPermission('poap:poapparticipantproject:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList" :projectId="props.projectId"></add-or-update>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {nextTick, onBeforeUpdate, reactive, ref, toRefs, watch} from "vue";
import AddOrUpdate from "./poapparticipantproject-add-or-update.vue";
import {ElMessageBox} from "element-plus";

const props = defineProps({
  projectId:String,
  projectStatus:Number
})
const view = reactive({
  getDataListURL: "/poap/poapparticipantproject/page",
  getDataListIsPage: true,
  exportURL: "/poap/poapparticipantproject/export",
  deleteURL: "/poap/poapparticipantproject",
  deleteIsBatch: true,
  drawerPart:false,//合作者抽屉
  buttonShow:true,
  dataForm: {
    participantId: "",
    projectId: "" as any,
  },
  roleText:[
    {value:0,label:"项目创建者"},
    {value:1,label:"项目负责人"},
    {value:2,label:"项目成员"},
    {value:3,label:"项目联系人"},
  ]
});

const state = reactive({ ...useView(view), ...toRefs(view) });


const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};
const initPart = ()=>{
  state.drawerPart = true;
  state.dataForm.projectId = props.projectId;
  // if (props.projectStatus === 1 || props.projectStatus === 2) this.buttonShow = false;
  refresh();
};
const refresh = ()=>{
  state.getDataList();
};
const handleClose = ()=> {
  ElMessageBox.confirm('确定要关闭吗？',{confirmButtonText:"确认",cancelButtonText:"取消"})
    .then(() => {
      state.drawerPart = false;
    })
};
const tableRowClassName = ({ row }:{row:any})=> {
  console.log(row);
  var n = "";
  if(row.role === 0){
    n = "zero"
  }else if (row.role === 1) {
    n = "first";
  } else if (row.role === 2) {
    n = "second";
  } else if (row.role === 3) {
    n = "three";
  }
  console.log(n);
  return n;
}
const getText = ()=>{
  state.dataList=state.dataList!.map((item,index)=>{
    return {...item,roleText:""}
  })
  for (let i = 0; i < state.dataList.length; i++) {
    if (state.dataList[i].role === 0) state.dataList[i].roleText = "项目创建者";
    else if (state.dataList[i].role === 1) state.dataList[i].roleText = "项目负责人";
    else if (state.dataList[i].role === 2) state.dataList[i].roleText = "项目成员";
    else if (state.dataList[i].role === 3) state.dataList[i].roleText = "项目联系人";

    console.log(state.dataList[i].roleText)
  }
  if(props.projectStatus!==0) state.buttonShow=false
}

watch(
  ()=>state.dataList,
  (newValue,oldValue)=>{
    getText()
  }
)

defineExpose({initPart})
</script>
<style>
.el-table .zero {
  background: #c8ffd8;
}
.el-table .first {
  background: #ffe1c8;
}
/*.el-table .second {*/
/*  background: #c8ecff;*/
/*}*/
.el-table .three {
  background: #dcc8ff;
}
</style>
